import nltk
import re
import rules_resolution as rules_resolution
import time

global acronyms
acronyms = {}

def acronymDict(sentences):
	pattern = r"\([A-Z]+\)"
	i = 0
	while i < len(sentences):
		acros = re.findall(pattern, sentences[i])
		if len(acros) > 0:
			words = nltk.word_tokenize(sentences[i])
			for acro in acros:
				acronym = acro[1:-1]
				j = 0
				k = 0
				astring = ""
				while j < len(words) and k < len(acronym):
					if words[j][0].lower() == acronym[k].lower():
						astring += words[j] + ' '
						k += 1
					else:
						astring = ""
						k = 0
					j += 1
				if astring.strip() != "":
					acronyms[acronym] = astring.strip()
		i += 1

def splitSentence(text):
	sentenceList = []
	
	tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
	sentenceList = tokenizer.tokenize(text, True)
	
	return sentenceList

def solveFile(filename):
	text = open(filename+".txt", 'r').read()
	sentences = splitSentence(text)
	acronymDict(sentences)
	
	reference = []
	begin = time.clock()
	
	i = 0
	while i < len(sentences):
		reference.append(sentences[i])
		temp = rules_resolution.applyRules(sentences[i], reference)
		if len(temp[1]) > 0:
			tmp = temp[0][temp[1][0]]
		else:
			tmp = sentences[i]
		reference[i] = tmp
		i += 1
	
	end = time.clock()
	print "ELAPSED TIME: "+str(end-begin)
	
	j = 0
	while j < len(reference):
		sent = reference[j]
		for short, long in acronyms.items():
			sent = sent.replace(short, long)
		reference[j] = sent
		j += 1
	
	file = open(filename+"_rule5.txt", 'w')
	for ref in reference:
		file.write(ref+"\n")
	file.close()

def testSolve(filename):
	lines = open(filename+".txt", 'r').readlines()
	sentences = []
	subheading = {}
	i = 0
	for line in lines:
		line = line.strip()
		if i == 0:
			title = line
			i += 1
		elif line.startswith("http"):
			link = line
			i += 1
		elif line.startswith("["):
			sentences.append(re.sub(r"\[\d+\]", "", line))
			i += 1
		elif line != "":
			subheading[i] = line
			i += 1
	acronyms.clear()
	acronymDict(sentences)
	
	reference = []
	begin = time.clock()
	
	i = 0
	while i < len(sentences):
		reference.append(sentences[i])
		temp = rules_resolution.applyRules(sentences[i], reference)
		if len(temp[1]) > 0:
			tmp = temp[0][temp[1][0]]
		else:
			tmp = sentences[i]
		reference[i] = tmp
		i += 1
	
	end = time.clock()
	print "ELAPSED TIME: "+str(end-begin)
	
	j = 0
	while j < len(reference):
		sent = reference[j]
		for short, long in acronyms.items():
			sent = sent.replace(short, long)
		reference[j] = sent
		j += 1
	
	j = 0
	file = open(filename+"_Coref_Auto.txt", 'w')
	file.write(title+"\n\n")
	j += 1
	file.write(link+"\n\n")
	j += 1
	i = 0
	for ref in reference:
		if j in subheading.keys():
			file.write(subheading[j]+"\n\n")
			j += 1
		file.write('['+str(i)+']'+ref+"\n\n")
		j += 1
		i += 1
	file.close()

	
if __name__ == '__main__':
	print 'start'
	# solveFile("test/training/8")
	testSolve("EngReferenceResolution/test/EN 1-30/30_EN")
	THEBEGINNING = time.clock()
	f = 1
	while f < 3:
		print "Running "+str(f)+"_EN..."
		testSolve("EngReferenceResolution/test/EN 1-30/"+str(f)+"_EN")
		f += 1
	THEEND = time.clock()
	print "TOTAL ELAPSED TIME: "+str(THEEND-THEBEGINNING)
	print "EVERAGE TIME PER NEWS: "+str((THEEND-THEBEGINNING)/30)
